package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void save(ShoppingCartDTO shoppingCartDTO) {
        //创建实体类对象
        ShoppingCart shoppingCart=new ShoppingCart();
        //获取用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //拷贝dto的数据拷贝进实体类对象
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取购物车数据
        List<ShoppingCart> list =shoppingCartMapper.list(shoppingCart);
        //如果有数据,菜品或者套餐的数量加一
        if(list != null && list.size()>0){
            //如果有数据只有可能是第一条
            shoppingCart= list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateNumber(shoppingCart);
        }
        //如果没有添加数据
        else {
            //判断添加的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            //菜品id不为空就是菜品
            if(dishId !=null){
                //根据菜品id查询菜品数据
                Dish dish = dishMapper.getById(dishId);
                //给对应的变量赋值
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            }
            else {
                Long setmealId = shoppingCartDTO.getSetmealId();
                //根据套餐id查询套餐数据
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            //插入数据
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart=ShoppingCart
                .builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        Long userId = shoppingCart.getUserId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车里的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShopppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //查询购物车数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        shoppingCart = list.get(0);
        Integer number = shoppingCart.getNumber();
        //如果商品数量不等于1直接减去一个
        if(number !=1){
            shoppingCart.setNumber(number-1);
            shoppingCartMapper.updateNumber(shoppingCart);
        }else {
            shoppingCartMapper.deleteDishOrSetmeal(shoppingCart);
        }
    }
}
